package com.jsm.tf.net.command;

import com.jsm.tf.entity.User;
import com.jsm.tf.net.message.request.LoginMessage;
import com.jsm.tf.net.message.response.LoginResponseMessage;
import com.jsm.tf.net.session.ISessionManager;
import com.jsm.tf.service.UserService;
import io.netty.channel.Channel;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class LoginCommand implements Command<LoginMessage> {
    @Resource
    private UserService userService;
    @Resource
    private ISessionManager sessionManager;
    @Override
    public void execute(LoginMessage message, Channel channel) {
        User user = userService.login(message.getUsername(), message.getPassword());
        LoginResponseMessage loginResponseMessage = new LoginResponseMessage();
        if(user!=null){
            loginResponseMessage.setUser(user);
            loginResponseMessage.setCode(200);
            loginResponseMessage.setSeq(message.getSeq());
            channel.writeAndFlush(loginResponseMessage);
            sessionManager.bind(user,channel);
        }
    }

}
